package com.rxyb.rabbitmq.toolmq.controller;

import com.rxyb.rabbitmq.toolmq.enums.QueueEnums;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @author YJH
 * @version 1.0
 * @description
 * @date 2020/6/29 17:21
 */
@RestController
public class InitController {

    private final RabbitTemplate rabbitTemplate;

    public InitController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @RequestMapping(value = "/init")
    public String init() {
        String msgId = String.valueOf(UUID.randomUUID());
        String sendTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String, Object> map = new HashMap<>(4);
        map.put("msgId", msgId);
        map.put("msgData", "test dead letter queue");
        map.put("sendTime", sendTime);
        rabbitTemplate.convertAndSend(QueueEnums.R_LOG_RECORD.getExchange(), QueueEnums.R_LOG_RECORD.getRouting(), map, message -> {
            message.getMessageProperties().setExpiration("10000");
            return message;
        });
//        rabbitTemplate.convertAndSend("TestDeadExchange", "TestDeadRouting", map);
        return "0000";
    }
}
